/*
 * Database.java
 *
 * Created on May 7, 2006, 8:47 AM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

/**
 *
 * @author jtma
 */

import java.util.Comparator;
import java.sql.*;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.util.Vector;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.*;
import java.util.HashSet;
import java.lang.reflect.Array;

public class Database 
{
    public class AthleteFileData
    {
        public String AthleteID;
        public String FirstName;
        public String LastName;
        public String Email;
        public String Gender;
        public String College;
        public String School;
        public String Experience;
        public String AgeGroup;
        public String Event;
        public String Nandu;
        public String SetupInfo;
        public String JudgeTypes;
        public String JudgeConnected;
        public String JudgeARaw;
        public String JudgeAAgree;
        public String JudgeBRaw;
        public String JudgeCRaw;
        public String JudgeCAgree;
        public String Time;
        public String Deductions;
        public String Reason;
        public String Score;
    };
    public static Vector AthleteList;
    
     //Athlete ID and entire event competed
    public class AthleteReport
    {
        public String AthleteID;
        public Vector AthleteEvents;
        
        public AthleteReport()
        {
            AthleteEvents=new Vector();
        }          
    };
    
    public double getDoubleValue(String data)
    {
        double dataDouble=0.0;
        try
        {
            dataDouble=Double.parseDouble(data);
        } catch (NumberFormatException e)
        {

        }
        return dataDouble;
    }
    
    //read the Input Text File where all the athletes info is saved
    public void readInputTextFile(String fileName)
    {
        String Extra;
        try
        {
            BufferedReader inputScript=new BufferedReader(new FileReader(fileName));
            do
            {
                AthleteFileData athlete=new  AthleteFileData();
                athlete.AthleteID=inputScript.readLine();
                if(athlete.AthleteID==null)
                    break;
                athlete.FirstName=inputScript.readLine();
                athlete.LastName=inputScript.readLine();
                athlete.Email=inputScript.readLine();
                athlete.Gender=inputScript.readLine();
                athlete.College=inputScript.readLine();
                athlete.School=inputScript.readLine();
                athlete.Experience=inputScript.readLine();
                athlete.AgeGroup=inputScript.readLine();
                athlete.Event=inputScript.readLine();
                athlete.Nandu=inputScript.readLine();
                athlete.SetupInfo=inputScript.readLine();
                athlete.JudgeTypes=inputScript.readLine();
                athlete.JudgeConnected=inputScript.readLine();
                athlete.JudgeARaw=inputScript.readLine();
                athlete.JudgeAAgree=inputScript.readLine();
                athlete.JudgeBRaw=inputScript.readLine();
                athlete.JudgeCRaw=inputScript.readLine();
                athlete.JudgeCAgree=inputScript.readLine();
                athlete.Time=inputScript.readLine();
                athlete.Deductions=inputScript.readLine();
                athlete.Reason=inputScript.readLine();
                athlete.Score=inputScript.readLine();
                
                //read extra line ---------------
                Extra=inputScript.readLine();
                
                //add the athlere list
                AthleteList.add(athlete);
            }while(true);
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
    
    public void writeOutputFile(String fileName)
    {
        PrintWriter outFile;
        try
        {
            outFile=new PrintWriter(new FileOutputStream(fileName)); 
            
            //print the tuples   
            for(int i=0;i<AthleteList.size();i++)
            {
                AthleteFileData athlete=(AthleteFileData)AthleteList.get(i);

                outFile.println(athlete.AthleteID);
                outFile.println(athlete.FirstName);
                outFile.println(athlete.LastName);
                outFile.println(athlete.Email);
                outFile.println(athlete.Gender);
                outFile.println(athlete.College);
                outFile.println(athlete.School);
                outFile.println(athlete.Experience);
                outFile.println(athlete.AgeGroup);
                outFile.println(athlete.Event);
                outFile.println(athlete.Nandu);
                outFile.println(athlete.SetupInfo);
                outFile.println(athlete.JudgeTypes);
                outFile.println(athlete.JudgeConnected);
                outFile.println(athlete.JudgeARaw);
                outFile.println(athlete.JudgeAAgree);
                outFile.println(athlete.JudgeBRaw);
                outFile.println(athlete.JudgeCRaw);
                outFile.println(athlete.JudgeCAgree);
                outFile.println(athlete.Time);
                outFile.println(athlete.Deductions);
                outFile.println(athlete.Reason);
                outFile.println(athlete.Score);
                outFile.println("--------------------------------------");
            }
            outFile.close();
        }
        catch(Exception e)
        {
            System.out.print("Error : " + e.toString());
        }
    
    }
    
    public Database()
    {
        AthleteList=new Vector();
        //read all the athletes
        readInputTextFile("../data/athletesSimple.txt");
    }
    
    //updates/save the athletes data
    public void Marshal(athlete a, int scoringSystem)
    {
        String athleteName=a.getName();
        //remove teh order
        athleteName=athleteName.substring(0, athleteName.indexOf("(")-1);
        
        int index=0;
        for(int i=0;i<AthleteList.size();i++)
        {
            AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(i);
            if(athleteName.equals(ad.FirstName+" "+ad.LastName) && a.isEvent(ad.Event))
            {
                //Copy Judges Related data
                ad.SetupInfo=a.getSetupInfo();
                ad.JudgeTypes=a.getJudgeTypeString();
                ad.JudgeConnected=a.getJudgeConnectedString();
                ad.JudgeARaw=a.MarshalRawA();
                ad.JudgeBRaw=a.MarshalRawB(scoringSystem);
                ad.JudgeCRaw=a.MarshalRawC();
                ad.JudgeAAgree=a.MarshalAgreeA();
                ad.JudgeCAgree=a.MarshalAgreeC();
                ad.Deductions=a.MarshalDeductions();
                ad.Reason=a.getReason();
                ad.Time=String.format("%d", globalData.diffTime);
                ad.Score=a.getFinalScore();
                break;
            }
        }
        //put a code to save it in the disk
        writeOutputFile("../data/athletesSimple.txt");
   
    }
    
    
    //updates/save the athletes data
    public void MarshalSimple(athlete a, int scoringSystem)
    {
        String athleteName=a.getName();
        //remove teh order
        athleteName=athleteName.substring(0, athleteName.indexOf("(")-1);
       
        for(int i=0;i<AthleteList.size();i++)
        {
            AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(i);
            if(athleteName.equals(ad.FirstName+" "+ad.LastName) && a.isEvent(ad.Event))
            {
                //Copy Judges Related data
                ad.SetupInfo=a.getSetupInfo();
                ad.JudgeTypes="0 0 0 1 1 1 2 2 2 1";
                ad.JudgeConnected="0 0 0 0 0 0 0 0 0 1";
                ad.JudgeARaw=a.sjJudgeA;
                ad.JudgeBRaw=a.sjJudgeB;
                ad.JudgeCRaw=a.sjJudgeC;
                ad.JudgeAAgree=a.sjJudgeASummary;
                ad.JudgeCAgree=a.sjJudgeCSummary;
                ad.Deductions=a.sjDeductions;
                ad.Reason=a.getReason();
                ad.Time=a.sjJudgeBSummary;//String.format("%d", globalData.diffTime);
                ad.Score=a.sjFinal;
                break;
            }
        }
        //put a code to save it in the disk
        writeOutputFile("../data/athletesSimple.txt");
   
    }
    
    //resets the athlete and saves the data to the disk
    //used to reset all the athlete data
    public void resetNSave(athlete a)
    {
        String athleteName=a.getName();
        //remove teh order
        athleteName=athleteName.substring(0, athleteName.indexOf("(")-1);
        
        int index=0;
        for(int i=0;i<AthleteList.size();i++)
        {
            AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(i);
            if(athleteName.equals(ad.FirstName+" "+ad.LastName) && a.isEvent(ad.Event))
            {
                //Clear all data related to athlete
                ad.SetupInfo="";
                ad.JudgeTypes="";
                ad.JudgeConnected="";
                ad.JudgeARaw="";
                ad.JudgeBRaw="";
                ad.JudgeCRaw="";
                ad.JudgeAAgree="";
                ad.JudgeCAgree="";
                ad.Deductions="";
                ad.Reason="";
                ad.Time="";
                ad.Score="";
                break;
            }
        }
        //put a code to save it in the disk
        writeOutputFile("../data/athletesSimple.txt");
   
    }
    
    //retrive the value from the database
    public void Unmarshal(athlete a)
    {
        String athleteName=a.getName();
        
        //remove teh order if present
        if(athleteName.indexOf("(")!=-1)
            athleteName=athleteName.substring(0, athleteName.indexOf("(")-1);
        
        int index=0;
        for(int i=0;i<AthleteList.size();i++)
        {
            AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(i);
            if(athleteName.equals(ad.FirstName+" "+ad.LastName) && a.isEvent(ad.Event))
            {
                //copy the Nandu
                a.setSetupInfo(ad.SetupInfo);
                a.setJudgeTypeString(ad.JudgeTypes);
                a.setJudgeConnectedString(ad.JudgeConnected);
                a.setNandu(ad.Nandu);
                //Copy Judges Related data
                a.UnmarshalRawA(ad.JudgeARaw);
                a.UnmarshalRawB(ad.JudgeBRaw);
                a.UnmarshalRawC(ad.JudgeCRaw);
                a.UnmarshalDeductions(ad.Deductions);
                if(ad.Time.length()!=0)
                    a.setTime(Integer.parseInt(ad.Time));
                //if(ad.Time.length()!=0)
                //    globalData.diffTime = Integer.parseInt(ad.Time);
                a.setReason(ad.Reason);
                break;
            }
        }
    }
    
    //retrive the value from the database
    public void UnmarshalSimple(athlete a)
    {
        String athleteName=a.getName();
        
        //remove teh order if present
        if(athleteName.indexOf("(")!=-1)
            athleteName=athleteName.substring(0, athleteName.indexOf("(")-1);
        
        for(int i=0;i<AthleteList.size();i++)
        {
            AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(i);
            if(athleteName.equals(ad.FirstName+" "+ad.LastName) && a.isEvent(ad.Event))
            {
                
                 //copy the Nandu
                a.setSetupInfo(ad.SetupInfo);
                a.setJudgeTypeString(ad.JudgeTypes);
                a.setJudgeConnectedString(ad.JudgeConnected);
                a.setNandu(ad.Nandu);
                a.sjJudgeA=ad.JudgeARaw;
                a.sjJudgeB=ad.JudgeBRaw;
                a.sjJudgeC=ad.JudgeCRaw;
                a.sjJudgeASummary=ad.JudgeAAgree;
                a.sjJudgeBSummary=ad.Time;
                a.sjJudgeCSummary=ad.JudgeCAgree;                
                //ad.JudgeCAgree;
                a.sjDeductions=ad.Deductions;
                a.setReason(ad.Reason);
                //if(ad.Time.length()!=0)
                //    a.setTime(Integer.parseInt(ad.Time));
                a.sjFinal=ad.Score;
                if(a.sjFinal.trim().length()>0)
                    a.UnmarshalRawB("(9,0.00)");
                
                break;
            }
        }
    }

    public HashSet getGroups()
    {
       HashSet groupList=new HashSet();

        for(int i=0;i<AthleteList.size();i++)
        {
            AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(i);
            String temp="/";
            String data=ad.AgeGroup+temp+ad.Experience+temp+ad.Gender;
            groupList.add(data);
        }

       return groupList;
    }
    
    //returns events
    public HashSet getEvents(String ageGroup, String level, String gender)
    {
        HashSet eventSet = new HashSet();
        
        for(int i=0;i<AthleteList.size();i++)
        {
            AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(i);
            if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level))
                eventSet.add(ad.Event);
        }
        return eventSet;
    }

    //returns report for an event
    private String getReport(String ageGroup, String level, String gender)
    {
        String report=new String();
        HashSet events = getEvents(ageGroup, level, gender);
        int i,j,k;
        if (events != null) 
        {
            for (i = 0; i < Array.getLength(globalData.eventInfo); i++) 
            {
                if (events.contains(globalData.eventInfo[i])) 
                {
                    Vector vec=new Vector();
                    Vector sortVec=new Vector();
                    for(k=0;k<AthleteList.size();k++)
                    {
                        AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(k);
                        if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level) && ad.Event.equals(globalData.eventInfo[i]))
                        {
                            vec.add(ad);
                        }
                    }
                    
                    //sort data
                    for (k = 0; k < vec.size();k++)
                        sortVec.add(k);
                    
                    for (j = 0; j < vec.size();j++)
                    {
                        for (k = j+1; k < vec.size();k++)
                        {
                            double a=0,b=0;
                            int x,y;
                            x=((Integer)sortVec.get(j));
                            y=((Integer)sortVec.get(k));
                            if(((AthleteFileData)vec.get(x)).Score.length()!=0)
                                a=getDoubleValue(((AthleteFileData)vec.get(x)).Score);
                            
                            if(((AthleteFileData)vec.get(y)).Score.length()!=0)
                                b=getDoubleValue(((AthleteFileData)vec.get(y)).Score);
                            
                            if(b>a)//switch
                            {
                                sortVec.set(j,y);
                                sortVec.set(k,x);
                            }
                            else if(b==a)
                            {
                                AthleteFileData aAthleteData=(AthleteFileData)AthleteList.elementAt(j);
                                athlete aAthlete=new athlete(level, gender,globalData.eventInfo[i], aAthleteData.FirstName+" "+aAthleteData.LastName+" (0)");
                                Unmarshal(aAthlete); //load all the data to the athlete stucture 
                                
                                AthleteFileData bAthleteData=(AthleteFileData)AthleteList.elementAt(k);
                                athlete bAthlete=new athlete(level, gender,globalData.eventInfo[i], bAthleteData.FirstName+" "+bAthleteData.LastName+" (0)");
                                Unmarshal(bAthlete); //load all the data to the athlete stucture 

                                //if the values are equal compute successful b level nandus
                                if(bAthlete.calculateSuccessfulBLevelOrGreaterNandu()>aAthlete.calculateSuccessfulBLevelOrGreaterNandu())
                                {
                                    sortVec.set(j,y);
                                    sortVec.set(k,x);                                    
                                }
                            }
                        }
                    }
                    
                    double lastScore=0.0f;
                     //print 
                    report+="Summary of "+ageGroup+" "+level+" "+gender+" "+globalData.eventInfo[i]+"\r\n";
                    report+="<TABLE border=\"2\" cellpadding=\"8\">";
                    
                        //get the type of the scoring sytem used
                        if(getScoringSystem(level,globalData.eventInfo[i])==0)// 0 Traditional System
                        {
                            report+="<TD> Competitor Name </TD>";
                            report+="<TD> College </TD>";
                            report+="<TD> School </TD>";
                            report+="<TD> Gender </TD>";
                            report+="<TD> Skill Level </TD>";
                            report+="<TD> Event </TD>";                            
                            report+="<TD> Score Summary </TD>";
                            report+="<TD> Cumulative Score </TD>";
                            report+="<TD> Deductions </TD>";
                            report+="<TD> Reason </TD>";
                            report+="<TD> Final Score </TD>";
                            report+="<TD> Ranking </TD>";

                            for (k = 0; k < vec.size();k++)
                            {
                                AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));

                                //create temporary athlete
                                athlete ath=new athlete(level, gender,globalData.eventInfo[i], ad.FirstName+" "+ad.LastName+" (0)");
                                //load all the data to the athlete stucture 
                                Unmarshal(ath);

                                if(ad.Score.length()!=0)
                                {
                                    report+="<TR>";
                                        report+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                                        report+="<TD> "+ad.College+" </TD>";
                                        report+="<TD> "+ad.School+" </TD>";
                                        report+="<TD> "+gender+" </TD>";
                                        report+="<TD> "+level+" </TD>";
                                        report+="<TD> "+globalData.eventInfo[i]+" </TD>";                                        
                                        report+="<TD> "+ath.getSummaryJudgeB(0)+" </TD>";
                                        report+="<TD> "+String.format("%.2f",ath.calculateBScore(0))+" </TD>";
                                        report+="<TD> "+ad.Deductions+" </TD>";
                                        report+="<TD> "+ad.Reason+" </TD>";                                        
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ad.Score))+" </TD>";
                                        if(lastScore==getDoubleValue(ad.Score))
                                            report+="<TD> "+(k)+" </TD>";
                                        else
                                            report+="<TD> "+(k+1)+" </TD>";
                                        lastScore=getDoubleValue(ad.Score);
                                    report+="</TR>";
                                }
                            }

                        }
                        else if(getScoringSystem(level,globalData.eventInfo[i])==1)// Nandu system with AB judges
                        {
                            report+="<TD> Competitor Name </TD>";
                            report+="<TD> College </TD>";
                            report+="<TD> School </TD>";
                            report+="<TD> Gender </TD>";
                            report+="<TD> Skill Level </TD>";
                            report+="<TD> Event </TD>";
                            report+="<TD> Judge A Summary </TD>";
                            report+="<TD> Judge B Summary </TD>";
                            report+="<TD> Judge A Score </TD>";
                            report+="<TD> Judge B Score </TD>";
                            report+="<TD> Deductions </TD>";
                            report+="<TD> Reason </TD>";                            
                            report+="<TD> Final Score </TD>";
                            report+="<TD> Ranking </TD>";
                            for (k = 0; k < vec.size();k++)
                            {
                                AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));

                                //create temporary athlete
                                athlete ath=new athlete(level, gender,globalData.eventInfo[i], ad.FirstName+" "+ad.LastName+" (0)");
                                //load all the data to the athlete stucture 
                                Unmarshal(ath);

                                if(ad.Score.length()!=0)
                                {
                                    report+="<TR>";
                                        report+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                                        report+="<TD> "+ad.College+" </TD>";
                                        report+="<TD> "+ad.School+" </TD>";
                                        report+="<TD> "+gender+" </TD>";
                                        report+="<TD> "+level+" </TD>";
                                        report+="<TD> "+globalData.eventInfo[i]+" </TD>";
                                        report+="<TD> "+ath.getSummaryJudgeA()+" </TD>";
                                        report+="<TD> "+ath.getSummaryJudgeB(2)+" </TD>";
                                        report+="<TD> "+String.format("%.2f",(ath.calculateAScore()))+" </TD>";
                                        report+="<TD> "+String.format("%.2f",(ath.calculateBScore(2)))+" </TD>";
                                        report+="<TD> "+ad.Deductions+" </TD>";
                                        report+="<TD> "+ad.Reason+" </TD>";
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ad.Score))+" </TD>";
                                        if(lastScore==getDoubleValue(ad.Score))
                                            report+="<TD> "+(k)+" </TD>";
                                        else
                                            report+="<TD> "+(k+1)+" </TD>";
                                        lastScore=getDoubleValue(ad.Score);
                                   report+="</TR>";
                                }
                            }
                        
                        }else// 2 Nandu System
                        {
                            report+="<TD> Competitor Name </TD>";
                            report+="<TD> College </TD>";
                            report+="<TD> School </TD>";
                            report+="<TD> Gender </TD>";
                            report+="<TD> Skill Level </TD>";
                            report+="<TD> Event </TD>";
                            report+="<TD> Judge A Summary </TD>";
                            report+="<TD> Judge B Summary </TD>";
                            report+="<TD> Judge C Summary </TD>";
                            report+="<TD> Successful >=B level nandu </TD>";
                            report+="<TD> Judge A Score </TD>";
                            report+="<TD> Judge B Score </TD>";
                            report+="<TD> Judge C Score </TD>";
                            report+="<TD> Deductions </TD>";
                            report+="<TD> Reason </TD>";                            
                            report+="<TD> Final Score </TD>";
                            report+="<TD> Ranking </TD>";
                            for (k = 0; k < vec.size();k++)
                            {
                                AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));

                                //create temporary athlete
                                athlete ath=new athlete(level, gender,globalData.eventInfo[i], ad.FirstName+" "+ad.LastName+" (0)");
                                //load all the data to the athlete stucture 
                                Unmarshal(ath);

                                if(ad.Score.length()!=0)
                                {
                                    report+="<TR>";
                                        report+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                                        report+="<TD> "+ad.College+" </TD>";
                                        report+="<TD> "+ad.School+" </TD>"; 
                                        report+="<TD> "+gender+" </TD>";
                                        report+="<TD> "+level+" </TD>";
                                        report+="<TD> "+globalData.eventInfo[i]+" </TD>";
                                        report+="<TD> "+ath.getSummaryJudgeA()+" </TD>";
                                        report+="<TD> "+ath.getSummaryJudgeB(2)+" </TD>";
                                        report+="<TD> "+ath.getSummaryNandu()+" </TD>";
                                        report+="<TD> "+ath.calculateSuccessfulBLevelOrGreaterNandu()+" </TD>";                                        
                                        report+="<TD> "+String.format("%.2f",(ath.calculateAScore()))+" </TD>";
                                        report+="<TD> "+String.format("%.2f",(ath.calculateBScore(2)))+" </TD>";
                                        report+="<TD> "+String.format("%.2f",(ath.calculateCScore()))+" </TD>";
                                        
                                        report+="<TD> "+ad.Deductions+" </TD>";
                                        report+="<TD> "+ad.Reason+" </TD>";
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ad.Score))+" </TD>";
                                        if(lastScore==getDoubleValue(ad.Score))
                                            report+="<TD> "+(k)+" </TD>";
                                        else
                                            report+="<TD> "+(k+1)+" </TD>";
                                        lastScore=getDoubleValue(ad.Score);
                                   report+="</TR>";
                                }
                            }

                        }
                    report+="</TABLE>";
                    report+="\r\n\r\n\r\n";
                    
                }
            } 
        }
        return report;        
    }
    
    //returns report for an event
    private String getScore(String ageGroup, String level, String gender)
    {
        String report=new String();
        HashSet events = getEvents(ageGroup, level, gender);
        int i,j,k;
        if (events != null) 
        {
            for (i = 0; i < Array.getLength(globalData.eventInfo); i++) 
            {
                if (events.contains(globalData.eventInfo[i])) 
                {
                    Vector vec=new Vector();
                    Vector sortVec=new Vector();
                    for(k=0;k<AthleteList.size();k++)
                    {
                        AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(k);
                        if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level) && ad.Event.equals(globalData.eventInfo[i]))
                        {
                            vec.add(ad);
                        }
                    }
                    
                    //sort data
                    for (k = 0; k < vec.size();k++)
                        sortVec.add(k);
                    
                    for (j = 0; j < vec.size();j++)
                    {
                        for (k = j+1; k < vec.size();k++)
                        {
                            double a=0,b=0;
                            int x,y;
                            x=((Integer)sortVec.get(j));
                            y=((Integer)sortVec.get(k));
                            if(((AthleteFileData)vec.get(x)).Score.length()!=0)
                                a=getDoubleValue(((AthleteFileData)vec.get(x)).Score);
                            
                            if(((AthleteFileData)vec.get(y)).Score.length()!=0)
                                b=getDoubleValue(((AthleteFileData)vec.get(y)).Score);
                            
                            if(b>a)//switch
                            {
                                sortVec.set(j,y);
                                sortVec.set(k,x);
                            }
                        }
                    }
                    
                    double lastScore=0.0f;
                     //print 
                    report+="Summary of "+ageGroup+" "+level+" "+gender+" "+globalData.eventInfo[i]+"\r\n";
                    report+="<TABLE border=\"2\" cellpadding=\"8\">";
                    
                        //get the type of the scoring sytem used
                        if(getScoringSystem(level,globalData.eventInfo[i])==0)// 0 Traditional System
                        {
                            report+="<TD> Competitor Name </TD>";
                            report+="<TD> College </TD>";
                            report+="<TD> School </TD>";
                            report+="<TD> Gender </TD>";
                            report+="<TD> Skill Level </TD>";
                            report+="<TD> Event </TD>";                            
                            //report+="<TD> Score Summary </TD>";
                            //report+="<TD> Cumulative Score </TD>";
                            //report+="<TD> Deductions </TD>";
                            //report+="<TD> Reason </TD>";
                            report+="<TD> Final Score </TD>";
                            report+="<TD> Ranking </TD>";

                            for (k = 0; k < vec.size();k++)
                            {
                                AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));

                                //create temporary athlete
                                athlete ath=new athlete(level, gender,globalData.eventInfo[i], ad.FirstName+" "+ad.LastName+" (0)");
                                //load all the data to the athlete stucture 
                                Unmarshal(ath);

                                if(ad.Score.length()!=0 && getDoubleValue(ad.Score)>0.0)
                                {
                                    report+="<TR>";
                                        report+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                                        report+="<TD> "+ad.College+" </TD>";
                                        report+="<TD> "+ad.School+" </TD>";
                                        report+="<TD> "+gender+" </TD>";
                                        report+="<TD> "+level+" </TD>";
                                        report+="<TD> "+globalData.eventInfo[i]+" </TD>";                                        
                                        //report+="<TD> "+ath.getSummaryJudgeB(0)+" </TD>";
                                        //report+="<TD> "+String.format("%.2f",getDoubleValue(ath.calculateBScore(0)))+" </TD>";
                                        //report+="<TD> "+ad.Deductions+" </TD>";
                                        //report+="<TD> "+ad.Reason+" </TD>";                                        
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ad.Score))+" </TD>";
                                        if(lastScore==getDoubleValue(ad.Score))
                                            report+="<TD> "+(k)+" </TD>";
                                        else
                                            report+="<TD> "+(k+1)+" </TD>";
                                        lastScore=getDoubleValue(ad.Score);
                                    report+="</TR>";
                                }
                            }

                        }
                        else if(getScoringSystem(level,globalData.eventInfo[i])==1)// Nandu system with AB judges
                        {
                            report+="<TD> Competitor Name </TD>";
                            report+="<TD> College </TD>";
                            report+="<TD> School </TD>";
                            report+="<TD> Gender </TD>";
                            report+="<TD> Skill Level </TD>";
                            report+="<TD> Event </TD>";
                            //report+="<TD> Judge A Summary </TD>";
                            //report+="<TD> Judge B Summary </TD>";
                            //report+="<TD> Judge A Score </TD>";
                            //report+="<TD> Judge B Score </TD>";
                            //report+="<TD> Deductions </TD>";
                            //report+="<TD> Reason </TD>";                            
                            report+="<TD> Final Score </TD>";
                            report+="<TD> Ranking </TD>";
                            for (k = 0; k < vec.size();k++)
                            {
                                AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));

                                //create temporary athlete
                                athlete ath=new athlete(level, gender,globalData.eventInfo[i], ad.FirstName+" "+ad.LastName+" (0)");
                                //load all the data to the athlete stucture 
                                Unmarshal(ath);

                                if(ad.Score.length()!=0 && getDoubleValue(ad.Score)>0.0)
                                {
                                    report+="<TR>";
                                        report+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                                        report+="<TD> "+ad.College+" </TD>";
                                        report+="<TD> "+ad.School+" </TD>";
                                        report+="<TD> "+gender+" </TD>";
                                        report+="<TD> "+level+" </TD>";
                                        report+="<TD> "+globalData.eventInfo[i]+" </TD>";
                                        //report+="<TD> "+ath.getSummaryJudgeA()+" </TD>";
                                        //report+="<TD> "+ath.getSummaryJudgeB(2)+" </TD>";
                                        //report+="<TD> "+String.format("%.2f",getDoubleValue(ath.calculateAScore()))+" </TD>";
                                        //report+="<TD> "+String.format("%.2f",getDoubleValue(ath.calculateBScore(2)))+" </TD>";
                                        //report+="<TD> "+ad.Deductions+" </TD>";
                                        //report+="<TD> "+ad.Reason+" </TD>";
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ad.Score))+" </TD>";
                                        if(lastScore==getDoubleValue(ad.Score))
                                            report+="<TD> "+(k)+" </TD>";
                                        else
                                            report+="<TD> "+(k+1)+" </TD>";
                                        lastScore=getDoubleValue(ad.Score);
                                   report+="</TR>";
                                }
                            }
                        
                        }else// 2 Nandu System
                        {
                            report+="<TD> Competitor Name </TD>";
                            report+="<TD> College </TD>";
                            report+="<TD> School </TD>";
                            report+="<TD> Gender </TD>";
                            report+="<TD> Skill Level </TD>";
                            report+="<TD> Event </TD>";
                            //report+="<TD> Judge A Summary </TD>";
                            //report+="<TD> Nandu Summary </TD>";
                            //report+="<TD> Judge B Summary </TD>";
                            //report+="<TD> Nandu Score </TD>";
                            //report+="<TD> Judge B Score </TD>";
                            //report+="<TD> Deductions </TD>";
                            //report+="<TD> Reason </TD>";                            
                            report+="<TD> Final Score </TD>";
                            report+="<TD> Ranking </TD>";
                            for (k = 0; k < vec.size();k++)
                            {
                                AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));

                                //create temporary athlete
                                athlete ath=new athlete(level, gender,globalData.eventInfo[i], ad.FirstName+" "+ad.LastName+" (0)");
                                //load all the data to the athlete stucture 
                                Unmarshal(ath);

                                if(ad.Score.length()!=0 && getDoubleValue(ad.Score)>0.0)
                                {
                                    report+="<TR>";
                                        report+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                                        report+="<TD> "+ad.College+" </TD>";
                                        report+="<TD> "+ad.School+" </TD>"; 
                                        report+="<TD> "+gender+" </TD>";
                                        report+="<TD> "+level+" </TD>";
                                        report+="<TD> "+globalData.eventInfo[i]+" </TD>";
                                        //report+="<TD> "+ath.getSummaryJudgeA()+" </TD>";
                                        //report+="<TD> "+ath.getSummaryNandu()+" </TD>";                                        
                                        //report+="<TD> "+ath.getSummaryJudgeB(2)+" </TD>";
                                        //report+="<TD> "+String.format("%.2f",getDoubleValue(ath.calculateCScore()))+" </TD>";
                                        //report+="<TD> "+String.format("%.2f",getDoubleValue(ath.calculateBScore(2)))+" </TD>";
                                        //report+="<TD> "+ad.Deductions+" </TD>";
                                        //report+="<TD> "+ad.Reason+" </TD>";
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ad.Score))+" </TD>";
                                        if(lastScore==getDoubleValue(ad.Score))
                                            report+="<TD> "+(k)+" </TD>";
                                        else
                                            report+="<TD> "+(k+1)+" </TD>";
                                        lastScore=getDoubleValue(ad.Score);
                                   report+="</TR>";
                                }
                            }

                        }
                    report+="</TABLE>";
                    report+="\r\n\r\n\r\n";
                    
                }
            } 
        }
        return report;        
    }
    
    public int getRankingSimple(athlete currAthlete, String event,String ageGroup, String level,String gender)
    {
        String athleteName=currAthlete.getName();
        //remove teh order
        athleteName=athleteName.substring(0, athleteName.indexOf("(")-1);
        
        int currAthletesRank=0;
                
        Vector vec=new Vector();
        Vector sortVec=new Vector();
        for(int k=0;k<AthleteList.size();k++)
        {
            AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(k);
            if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level) && ad.Event.equals(event))
            {
                vec.add(ad);
            }
        }

        //sort data
        for (int k = 0; k < vec.size();k++)
            sortVec.add(k);

        for (int j = 0; j < vec.size();j++)
        {
            for (int k = j+1; k < vec.size();k++)
            {
                double a=0,b=0;
                int x,y;
                x=((Integer)sortVec.get(j));
                y=((Integer)sortVec.get(k));
                if(((AthleteFileData)vec.get(x)).Score.length()!=0)
                    a=getDoubleValue(((AthleteFileData)vec.get(x)).Score);

                if(((AthleteFileData)vec.get(y)).Score.length()!=0)
                    b=getDoubleValue(((AthleteFileData)vec.get(y)).Score);

                if(b>a)//switch
                {
                    sortVec.set(j,y);
                    sortVec.set(k,x);
                }
                else if(b==a)
                {
                    AthleteFileData aAthleteData=(AthleteFileData)AthleteList.elementAt(j);
                    athlete aAthlete=new athlete(level, gender,event, aAthleteData.FirstName+" "+aAthleteData.LastName+" (0)");
                    UnmarshalSimple(aAthlete); //load all the data to the athlete stucture 

                    AthleteFileData bAthleteData=(AthleteFileData)AthleteList.elementAt(k);
                    athlete bAthlete=new athlete(level, gender,event, bAthleteData.FirstName+" "+bAthleteData.LastName+" (0)");
                    UnmarshalSimple(bAthlete); //load all the data to the athlete stucture 

                    //if the values are equal compute successful b level nandus
                    if(bAthlete.calculateSuccessfulBLevelOrGreaterNandu()>aAthlete.calculateSuccessfulBLevelOrGreaterNandu())
                    {
                        sortVec.set(j,y);
                        sortVec.set(k,x);                                    
                    }
                }
            }
        }
        
        for (int k = 0; k < vec.size();k++)
        {
            AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));
            if(athleteName.equals(ad.FirstName+" "+ad.LastName)==true)
                return (k+1);
        }        
        return currAthletesRank;
    }
    
    public int getRanking(athlete currAthlete, String event,String ageGroup, String level,String gender)
    {
        String athleteName=currAthlete.getName();
        //remove teh order
        athleteName=athleteName.substring(0, athleteName.indexOf("(")-1);
        
        int currAthletesRank=0;
                
        Vector vec=new Vector();
        Vector sortVec=new Vector();
        for(int k=0;k<AthleteList.size();k++)
        {
            AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(k);
            if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level) && ad.Event.equals(event))
            {
                vec.add(ad);
            }
        }

        //sort data
        for (int k = 0; k < vec.size();k++)
            sortVec.add(k);

        for (int j = 0; j < vec.size();j++)
        {
            for (int k = j+1; k < vec.size();k++)
            {
                double a=0,b=0;
                int x,y;
                x=((Integer)sortVec.get(j));
                y=((Integer)sortVec.get(k));
                if(((AthleteFileData)vec.get(x)).Score.length()!=0)
                    a=getDoubleValue(((AthleteFileData)vec.get(x)).Score);

                if(((AthleteFileData)vec.get(y)).Score.length()!=0)
                    b=getDoubleValue(((AthleteFileData)vec.get(y)).Score);

                if(b>a)//switch
                {
                    sortVec.set(j,y);
                    sortVec.set(k,x);
                }
                else if(b==a)
                {
                    AthleteFileData aAthleteData=(AthleteFileData)AthleteList.elementAt(j);
                    athlete aAthlete=new athlete(level, gender,event, aAthleteData.FirstName+" "+aAthleteData.LastName+" (0)");
                    Unmarshal(aAthlete); //load all the data to the athlete stucture 

                    AthleteFileData bAthleteData=(AthleteFileData)AthleteList.elementAt(k);
                    athlete bAthlete=new athlete(level, gender,event, bAthleteData.FirstName+" "+bAthleteData.LastName+" (0)");
                    Unmarshal(bAthlete); //load all the data to the athlete stucture 

                    //if the values are equal compute successful b level nandus
                    if(bAthlete.calculateSuccessfulBLevelOrGreaterNandu()>aAthlete.calculateSuccessfulBLevelOrGreaterNandu())
                    {
                        sortVec.set(j,y);
                        sortVec.set(k,x);                                    
                    }
                }
            }
        }
        
        for (int k = 0; k < vec.size();k++)
        {
            AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));
            if(athleteName.equals(ad.FirstName+" "+ad.LastName)==true)
                return (k+1);
        }        
        return currAthletesRank;
    }
    
    //returns report for an event for team selection team A, team B and team C
    private String getRankingSimple(String ageGroup, String level,String gender)
    {
        String report=new String();
        HashSet events = getEvents(ageGroup,level, gender);
        int i,j,k;
        if (events != null) 
        {
            //do grouping for team selection team A, team B and team C
            Vector list = new Vector();
            //Vector listScoreBareHand = new Vector();
            //Vector listScoreOther = new Vector();
            //int index=0;
            
            //generate list of athlte matching gender and level
            for(i=0;i<AthleteList.size();i++)
            {
                AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(i);
                if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level))
                {
                    boolean alreadyInTheList=false;
                    for(j=0;j<list.size();j++)
                    {
                        if(((AthleteFileData)list.get(j)).AthleteID.equals(ad.AthleteID))
                        {
                            alreadyInTheList=true;
                            break;
                        }                         
                    }
                    if(!alreadyInTheList)
                        list.addElement(ad);
                }
            }
        
            Vector finalScoreList=new Vector();
            Vector indexList=new Vector();
            Vector reportList=new Vector();
             
            //process all the athlete in that list
            for (i=0;i<list.size();i++)
            {
                AthleteFileData ad=(AthleteFileData)list.get(i);
                AthleteReport athleteReport=getAthleteReport(ad.AthleteID);                    
                boolean firstBareHand=true,firstOther=true;
                double bareHandScore=0.0,otherScore=0.0;
                String bareHandEventName="",otherEventName="";
                String Output=new String();
                Output+="<TR> ";
                    Output+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                    for(j=0;j<athleteReport.AthleteEvents.size();j++)
                    {
                        AthleteFileData singleEvent=(AthleteFileData)(athleteReport.AthleteEvents).get(j);

                        //is the event has not been scored skip
                        if(singleEvent.Score.length()==0)
                            continue;

                        //check if the event is bare hand
                        if(isBareHandEvent(singleEvent.Event))
                        {
                            if(firstBareHand)
                            {   
                                bareHandScore=getDoubleValue(singleEvent.Score);                            
                                bareHandEventName=singleEvent.Event;
                                firstBareHand=false;
                            }
                            else
                            {
                                if(bareHandScore<getDoubleValue(singleEvent.Score))
                                {
                                    bareHandScore=getDoubleValue(singleEvent.Score); 
                                    bareHandEventName=singleEvent.Event;
                                }
                            }
                        }
                        else
                        {
                            if(firstOther)
                            {   
                                otherScore=getDoubleValue(singleEvent.Score);   
                                otherEventName=singleEvent.Event;
                                firstOther=false;
                            }
                            else
                            {
                                if(otherScore<getDoubleValue(singleEvent.Score))
                                {
                                    otherScore=getDoubleValue(singleEvent.Score);
                                    otherEventName=singleEvent.Event;
                                }
                            }
                        }
                    }
                    Output+="<TD> "+"\t"+bareHandEventName+" </TD>"; 
                    Output+="<TD> "+String.format("%.2f",bareHandScore)+" </TD>";
                    Output+="<TD> "+"\t"+otherEventName+" </TD>";                           
                    Output+="<TD> "+String.format("%.2f",otherScore)+" </TD>";                    
                    Output+="<TD> "+String.format("%.2f",(bareHandScore+otherScore)*.5)+" </TD>";
                //Output+="</TR> "; //will be added later with ranks

                //used later for sorting the lists
                finalScoreList.add(((bareHandScore+otherScore)*.5));
                indexList.add(i);
                reportList.add(Output);
            }

            for (j = 0; j < finalScoreList.size();j++)
            {
                for (k = j+1; k < finalScoreList.size();k++)
                {
                    double a=0,b=0;
                    int x,y;

                    x=((Integer)indexList.get(j));
                    y=((Integer)indexList.get(k));

                    a=((Double)finalScoreList.get(x));
                    b=((Double)finalScoreList.get(y));

                    if(b>a)//switch
                    {
                        indexList.set(j,y);
                        indexList.set(k,x);
                    }
                }
            }
           
            //no athlete just return 
            if(indexList.size()==0)
                return report;
            
            report="Athletes ranking of "+ageGroup+" "+level+" "+gender+"\r\n";
            report+="<TABLE border=\"2\" cellpadding=\"8\">";
                report+="<TD> Name </TD>";
                report+="<TD> Bare Hand Event </TD>";
                report+="<TD> Bare Hand Score </TD>";
                report+="<TD> Other Event </TD>";
                report+="<TD> Other Score </TD>";
                report+="<TD> Final Score </TD>";
                report+="<TD> Rank </TD>";
                
                for (j = 0; j < indexList.size();j++)
                {
                    k=((Integer)indexList.get(j));
                    report+=((String)reportList.get(k))+"<TD> "+(j+1)+" </TD>"+" </TR>";
                }
                
            report+="</TABLE>";
            report+="\r\n\r\n\r\n";
            
        }
        return report;        
    }
    //returns report for an event for team selection team A, team B and team C
    private String getRanking(String ageGroup, String level,String gender)
    {
        String report=new String();
        HashSet events = getEvents(ageGroup,level, gender);
        int i,j,k;
        if (events != null) 
        {
            //do grouping for team selection team A, team B and team C
            Vector list = new Vector();
            Vector listScoreBareHand = new Vector();
            Vector listScoreOther = new Vector();
            
            int index=0;
            //generate list of athlte matching gender and level
            for(i=0;i<AthleteList.size();i++)
            {
                AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(i);
                if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level))
                {
                    boolean alreadyInTheList=false;
                    for(j=0;j<list.size();j++)
                    {
                        if(((AthleteFileData)list.get(j)).AthleteID.equals(ad.AthleteID))
                        {
                            alreadyInTheList=true;
                            break;
                        }                         
                    }
                    if(!alreadyInTheList)
                        list.addElement(ad);
                }
            }
        
            Vector finalScoreList=new Vector();
            Vector indexList=new Vector();
            Vector reportList=new Vector();
             
            //process all the athlete in that list
            for (i=0;i<list.size();i++)
            {
                AthleteFileData ad=(AthleteFileData)list.get(i);
                AthleteReport athleteReport=getAthleteReport(ad.AthleteID);                    
                boolean firstBareHand=true,firstOther=true;
                double bareHandScore=0.0,otherScore=0.0;
                String bareHandEventName="",otherEventName="";
                String Output=new String();
                Output+="<TR> ";
                    Output+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                    for(j=0;j<athleteReport.AthleteEvents.size();j++)
                    {
                        AthleteFileData singleEvent=(AthleteFileData)(athleteReport.AthleteEvents).get(j);

                        //is the event has not been scored skip
                        if(singleEvent.Score.length()==0)
                            continue;

                        //check if the event is bare hand
                        if(isBareHandEvent(singleEvent.Event))
                        {
                            if(firstBareHand)
                            {   
                                bareHandScore=getDoubleValue(singleEvent.Score);                            
                                bareHandEventName=singleEvent.Event;
                                firstBareHand=false;
                            }
                            else
                            {
                                if(bareHandScore<getDoubleValue(singleEvent.Score))
                                {
                                    bareHandScore=getDoubleValue(singleEvent.Score); 
                                    bareHandEventName=singleEvent.Event;
                                }
                            }
                        }
                        else
                        {
                            if(firstOther)
                            {   
                                otherScore=getDoubleValue(singleEvent.Score);   
                                otherEventName=singleEvent.Event;
                                firstOther=false;
                            }
                            else
                            {
                                if(otherScore<getDoubleValue(singleEvent.Score))
                                {
                                    otherScore=getDoubleValue(singleEvent.Score);
                                    otherEventName=singleEvent.Event;
                                }
                            }
                        }
                    }
                    Output+="<TD> "+"\t"+bareHandEventName+" </TD>"; 
                    Output+="<TD> "+String.format("%.2f",bareHandScore)+" </TD>";
                    Output+="<TD> "+"\t"+otherEventName+" </TD>";                           
                    Output+="<TD> "+String.format("%.2f",otherScore)+" </TD>";                    
                    Output+="<TD> "+String.format("%.2f",(bareHandScore+otherScore)*.5)+" </TD>";
                //Output+="</TR> "; //will be added later with ranks

                //used later for sorting the lists
                finalScoreList.add(((bareHandScore+otherScore)*.5));
                indexList.add(i);
                reportList.add(Output);
            }

            for (j = 0; j < finalScoreList.size();j++)
            {
                for (k = j+1; k < finalScoreList.size();k++)
                {
                    double a=0,b=0;
                    int x,y;

                    x=((Integer)indexList.get(j));
                    y=((Integer)indexList.get(k));

                    a=((Double)finalScoreList.get(x));
                    b=((Double)finalScoreList.get(y));

                    if(b>a)//switch
                    {
                        indexList.set(j,y);
                        indexList.set(k,x);
                    }
                }
            }
           
            //no athlete just return 
            if(indexList.size()==0)
                return report;
            
            report="Athletes ranking of "+ageGroup+" "+level+" "+gender+"\r\n";
            report+="<TABLE border=\"2\" cellpadding=\"8\">";
                report+="<TD> Name </TD>";
                report+="<TD> Bare Hand Event </TD>";
                report+="<TD> Bare Hand Score </TD>";
                report+="<TD> Other Event </TD>";
                report+="<TD> Other Score </TD>";
                report+="<TD> Final Score </TD>";
                report+="<TD> Rank </TD>";
                
                for (j = 0; j < indexList.size();j++)
                {
                    k=((Integer)indexList.get(j));
                    report+=((String)reportList.get(k))+"<TD> "+(j+1)+" </TD>"+" </TR>";
                }
                
            report+="</TABLE>";
            report+="\r\n\r\n\r\n";
            
        }
        return report;        
    }

    //checks if the string array contains the string
    private boolean doesContain(String strArray[],String str)
    {
        for(int i=0;i<strArray.length;i++)
        {
            if(strArray[i].trim().toLowerCase().equals(str.toLowerCase())==true)
                return true;
        }
        return false;
    }
    
    //returns report for an event for team selection team A, team B and team C
    private String getEventRanking(String ageGroup, String level,String gender,String eventGroup1[], String eventGroup2[])
    {
        String report=new String();
        HashSet events = getEvents(ageGroup,level, gender);
        if (events == null) 
            return report;
        
        //do grouping for team selection team A, team B and team C
        Vector list = new Vector();

        //generate list of athlte matching gender and level
        for(int i=0;i<AthleteList.size();i++)
        {
            AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(i);
            if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level) && 
                    (doesContain(eventGroup1,ad.Event) || doesContain(eventGroup2,ad.Event)))
            {
                boolean alreadyInTheList=false;
                for(int j=0;j<list.size();j++)
                {
                    if(((AthleteFileData)list.get(j)).AthleteID.equals(ad.AthleteID))
                    {
                        alreadyInTheList=true;
                        break;
                    }                         
                }
                if(!alreadyInTheList)
                    list.addElement(ad);
            }
        }

        Vector finalScoreList=new Vector();
        Vector indexList=new Vector();
        Vector reportList=new Vector();

        //process all the athlete in that list
        for (int i=0;i<list.size();i++)
        {
            AthleteFileData ad=(AthleteFileData)list.get(i);
            AthleteReport athleteReport=getAthleteReport(ad.AthleteID);                    
            boolean isFirstList1Data=true,isFirstList2Data=true;
            double list1Score=0.0,list2Score=0.0;
            String highestList1EventName="",highestList2EventName="";
            String Output=new String();
           
            for(int j=0;j<athleteReport.AthleteEvents.size();j++)
            {
                AthleteFileData singleEvent=(AthleteFileData)(athleteReport.AthleteEvents).get(j);

                //is the event has not been scored skip
                if(singleEvent.Score.length()==0)
                    continue;

                //check if the event is in the first list
                if(doesContain(eventGroup1,singleEvent.Event))
                {
                    if(isFirstList1Data)
                    {   
                        list1Score=getDoubleValue(singleEvent.Score);                            
                        highestList1EventName=singleEvent.Event;
                        isFirstList1Data=false;
                    }
                    else
                    {
                        if(list1Score<getDoubleValue(singleEvent.Score))
                        {
                            //hack: if the second list is same too, move the highens number
                            if(doesContain(eventGroup2,singleEvent.Event) && list2Score<getDoubleValue(singleEvent.Score))
                            {
                                list2Score=list1Score;
                                highestList2EventName=highestList1EventName;
                            }

                            list1Score=getDoubleValue(singleEvent.Score); 
                            highestList1EventName=singleEvent.Event;
                        }
                    }
                }

                //check if the event is in the second list and not the highest in the first list
                if(highestList1EventName.equals(singleEvent.Event)==false && doesContain(eventGroup2,singleEvent.Event))
                {
                    if(isFirstList2Data)
                    {   
                        list2Score=getDoubleValue(singleEvent.Score);   
                        highestList2EventName=singleEvent.Event;
                        isFirstList2Data=false;
                    }
                    else
                    {
                        if(list2Score<getDoubleValue(singleEvent.Score))
                        {
                            list2Score=getDoubleValue(singleEvent.Score);
                            highestList2EventName=singleEvent.Event;
                        }
                    }
                }
            }
            
            //if the data belongs to the current events
            
            {
                Output+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                Output+="<TD> "+"\t"+highestList1EventName+" </TD>"; 
                Output+="<TD> "+String.format("%.2f",list1Score)+" </TD>";
                Output+="<TD> "+"\t"+highestList2EventName+" </TD>";                           
                Output+="<TD> "+String.format("%.2f",list2Score)+" </TD>";                    
                Output+="<TD> "+String.format("%.2f",(list1Score+list2Score))+" </TD>";
                //used later for sorting the lists
                finalScoreList.add(((list1Score+list2Score)*.5));
                indexList.add(i);
                reportList.add(Output);
            }
        }

        for (int j = 0; j < finalScoreList.size();j++)
        {
            for (int k = j+1; k < finalScoreList.size();k++)
            {
                double a=0,b=0;
                int x,y;

                x=((Integer)indexList.get(j));
                y=((Integer)indexList.get(k));

                a=((Double)finalScoreList.get(x));
                b=((Double)finalScoreList.get(y));

                if(b>a)//switch
                {
                    indexList.set(j,y);
                    indexList.set(k,x);
                }
            }
        }

        //no athlete just return 
        if(indexList.size()==0)
            return report;
        
        String eventGroup1Name="";
        for(int i=0;i<eventGroup1.length;i++)
        {
            if(i>0)
                eventGroup1Name+="/";
            eventGroup1Name+=eventGroup1[i];
        }
        
        String eventGroup2Name="";
        for(int i=0;i<eventGroup2.length;i++)
        {
            if(i>0)
                eventGroup2Name+="/";
            eventGroup2Name+=eventGroup2[i];
        }
        

        report="Athletes ranking of "+ageGroup+" "+level+" "+gender+"\r\n";
        report+="<TABLE border=\"2\" cellpadding=\"8\">";
            report+="<TD> Name </TD>";
            report+="<TD> "+eventGroup1Name+" Event </TD>";
            report+="<TD> "+eventGroup1Name+" Score </TD>";
            report+="<TD> "+eventGroup2Name+" Event </TD>";
            report+="<TD> "+eventGroup2Name+" Score </TD>";
            report+="<TD> Final Score </TD>";
            report+="<TD> Rank </TD>";

            for (int j = 0; j < indexList.size();j++)
            {
                int k=((Integer)indexList.get(j));
                report+="<TR> "+((String)reportList.get(k))+"<TD> "+(j+1)+" </TD>"+" </TR>";
            }

        report+="</TABLE>";
        report+="\r\n\r\n\r\n";
        return report;    
    }
    
    //computes final report
    public String getReport()
    {
       
        String report=new String();
        //print title
        report+="<font size=\"20\">Athletes Report\n\n</font>";

        for(int i=0;i< Array.getLength(globalData.AgeGroupList);i++)
        {
            for(int j=0;j< Array.getLength(globalData.LevelList);j++)
            {
                for(int k=0;k< Array.getLength(globalData.GenderList);k++)
                {
                    report+=getReport(globalData.AgeGroupList[i],globalData.LevelList[j], globalData.GenderList[k]);
                }
            }
        }
  
        
        report+="<font size=\"20\">Team Trials Selection\n\n</font>";
        for(int i=0;i< Array.getLength(globalData.AgeGroupList);i++)
        {
            for(int j=0;j< Array.getLength(globalData.LevelList);j++)
            {
                for(int k=0;k< Array.getLength(globalData.GenderList);k++)
                {
                    
                    
                    String eventGroup1[][]={
                                                {"Changquan"},
                                                {"Broadsword"},
                                                {"Straightsword"},
						{"Nanquan","NanquanC","Nandao","Nangun"},
						{"TaijiWeapon","Taijiquan","TaijiquanC"},
                                            };
                    String eventGroup2[][]={
						{"ChangquanC"},
						{"Staff"},
						{"Spear"},
						{"Nanquan","NanquanC","Nandao","Nangun"},
						{"TaijiWeapon","Taijiquan","TaijiquanC"},
                                            };
                    for(int l=0;l<eventGroup1.length;l++)
                        report+=getEventRanking(globalData.AgeGroupList[i],globalData.LevelList[j], globalData.GenderList[k],eventGroup1[l],eventGroup2[l]);
                }
            }
        }
        
//        //Seperate TEAM A, TEAM B, TEAM C
//        
//        //print title
//        report+="<font size=\"20\">Athletes Ranking\n\n</font>";
//
//        for(int i=0;i< Array.getLength(globalData.AgeGroupList);i++)
//        {
//            for(int j=0;j< Array.getLength(globalData.LevelList);j++)
//            {
//                for(int k=0;k< Array.getLength(globalData.GenderList);k++)
//                {
//                    report+=getRanking(globalData.AgeGroupList[i],globalData.LevelList[j], globalData.GenderList[k]);
//                }
//            }
//        }

        return report;
    }
    
    
    //returns report for an event
    private String getReportSimple(String ageGroup, String level, String gender)
    {
        String report=new String();
        HashSet events = getEvents(ageGroup, level, gender);
        int i,j,k;
        if (events != null) 
        {
            for (i = 0; i < Array.getLength(globalData.eventInfo); i++) 
            {
                if (events.contains(globalData.eventInfo[i])) 
                {
                    Vector vec=new Vector();
                    Vector sortVec=new Vector();
                    for(k=0;k<AthleteList.size();k++)
                    {
                        AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(k);
                        if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level) && ad.Event.equals(globalData.eventInfo[i]))
                        {
                            vec.add(ad);
                        }
                    }
                    
                    //sort data
                    for (k = 0; k < vec.size();k++)
                        sortVec.add(k);
                    
                    for (j = 0; j < vec.size();j++)
                    {
                        for (k = j+1; k < vec.size();k++)
                        {
                            double a=0,b=0;
                            int x,y;
                            x=((Integer)sortVec.get(j));
                            y=((Integer)sortVec.get(k));
                            if(((AthleteFileData)vec.get(x)).Score.length()!=0)
                                a=getDoubleValue(((AthleteFileData)vec.get(x)).Score);
                            
                            if(((AthleteFileData)vec.get(y)).Score.length()!=0)
                                b=getDoubleValue(((AthleteFileData)vec.get(y)).Score);
                            
                            if(b>a)//switch
                            {
                                sortVec.set(j,y);
                                sortVec.set(k,x);
                            }
                            else if(b==a)
                            {
                                AthleteFileData aAthleteData=(AthleteFileData)AthleteList.elementAt(j);
                                athlete aAthlete=new athlete(level, gender,globalData.eventInfo[i], aAthleteData.FirstName+" "+aAthleteData.LastName+" (0)");
                                UnmarshalSimple(aAthlete); //load all the data to the athlete stucture 
                                
                                AthleteFileData bAthleteData=(AthleteFileData)AthleteList.elementAt(k);
                                athlete bAthlete=new athlete(level, gender,globalData.eventInfo[i], bAthleteData.FirstName+" "+bAthleteData.LastName+" (0)");
                                UnmarshalSimple(bAthlete); //load all the data to the athlete stucture 

                                //if the values are equal compute successful b level nandus
                                if(bAthlete.calculateSuccessfulBLevelOrGreaterNandu()>aAthlete.calculateSuccessfulBLevelOrGreaterNandu())
                                {
                                    sortVec.set(j,y);
                                    sortVec.set(k,x);                                    
                                }
                            }
                        }
                    }
                    
                    double lastScore=0.0f;
                     //print 
                    report+="Summary of "+ageGroup+" "+level+" "+gender+" "+globalData.eventInfo[i]+"\r\n";
                    report+="<TABLE border=\"2\" cellpadding=\"8\">";
                    
                        //get the type of the scoring sytem used
                        if(getScoringSystem(level,globalData.eventInfo[i])==0)// 0 Traditional System
                        {
                            report+="<TD> Competitor Name </TD>";
                            report+="<TD> College </TD>";
                            report+="<TD> School </TD>";
                            report+="<TD> Gender </TD>";
                            report+="<TD> Skill Level </TD>";
                            report+="<TD> Event </TD>";                            
                            report+="<TD> Judge B Score </TD>";
                            report+="<TD> Summary B </TD>";
                            report+="<TD> Deductions </TD>";
                            report+="<TD> Reason </TD>";                            
                            report+="<TD> Final Score </TD>";                            
                            
                            report+="<TD> Ranking </TD>";
                            for (k = 0; k < vec.size();k++)
                            {
                                AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));

                                //create temporary athlete
                                athlete ath=new athlete(level, gender,globalData.eventInfo[i], ad.FirstName+" "+ad.LastName+" (0)");
                                //load all the data to the athlete stucture 
                                UnmarshalSimple(ath);

                                if(ad.Score.length()!=0)
                                {
                                    report+="<TR>";
                                        report+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                                        report+="<TD> "+ad.College+" </TD>";
                                        report+="<TD> "+ad.School+" </TD>"; 
                                        report+="<TD> "+gender+" </TD>";
                                        report+="<TD> "+level+" </TD>";
                                        report+="<TD> "+globalData.eventInfo[i]+" </TD>";
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ath.getBScoreSimple()))+" </TD>";
                                        report+="<TD> "+ad.JudgeAAgree+" </TD>";
                                        report+="<TD> "+ad.Deductions+" </TD>";
                                        report+="<TD> "+ad.Reason+" </TD>";
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ad.Score))+" </TD>";
                                        if(lastScore==getDoubleValue(ad.Score))
                                            report+="<TD> "+(k)+" </TD>";
                                        else
                                            report+="<TD> "+(k+1)+" </TD>";
                                        lastScore=getDoubleValue(ad.Score);
                                   report+="</TR>";
                                }
                            }
                        }
                        else if(getScoringSystem(level,globalData.eventInfo[i])==1)// Nandu system with AB judges
                        {
                            report+="<TD> Competitor Name </TD>";
                            report+="<TD> College </TD>";
                            report+="<TD> School </TD>";
                            report+="<TD> Gender </TD>";
                            report+="<TD> Skill Level </TD>";
                            report+="<TD> Event </TD>";                            
                            report+="<TD> Judge A Score </TD>";
                            report+="<TD> Judge B Score </TD>";
                            report+="<TD> Summary A </TD>";
                            report+="<TD> Summary B</TD>";
                            report+="<TD> Deductions </TD>";
                            report+="<TD> Reason </TD>";                            
                            report+="<TD> Final Score </TD>";
                            
                            
                            report+="<TD> Ranking </TD>";
                            for (k = 0; k < vec.size();k++)
                            {
                                AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));

                                //create temporary athlete
                                athlete ath=new athlete(level, gender,globalData.eventInfo[i], ad.FirstName+" "+ad.LastName+" (0)");
                                //load all the data to the athlete stucture 
                                UnmarshalSimple(ath);

                                if(ad.Score.length()!=0)
                                {
                                    report+="<TR>";
                                        report+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                                        report+="<TD> "+ad.College+" </TD>";
                                        report+="<TD> "+ad.School+" </TD>"; 
                                        report+="<TD> "+gender+" </TD>";
                                        report+="<TD> "+level+" </TD>";
                                        report+="<TD> "+globalData.eventInfo[i]+" </TD>";
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ath.getAScoreSimple()))+" </TD>";
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ath.getBScoreSimple()))+" </TD>";
                                        report+="<TD> "+ad.JudgeAAgree+" </TD>";
                                        report+="<TD> "+ad.Time+" </TD>";
                                        report+="<TD> "+ad.Deductions+" </TD>";
                                        report+="<TD> "+ad.Reason+" </TD>";
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ad.Score))+" </TD>";
                                        if(lastScore==getDoubleValue(ad.Score))
                                            report+="<TD> "+(k)+" </TD>";
                                        else
                                            report+="<TD> "+(k+1)+" </TD>";
                                        lastScore=getDoubleValue(ad.Score);
                                   report+="</TR>";
                                }
                            }
                        
                        }else// 2 Nandu System
                        {
                            report+="<TD> Competitor Name </TD>";
                            report+="<TD> College </TD>";
                            report+="<TD> School </TD>";
                            report+="<TD> Gender </TD>";
                            report+="<TD> Skill Level </TD>";
                            report+="<TD> Event </TD>";                            
                            report+="<TD> Judge A Score </TD>";
                            report+="<TD> Judge B Score </TD>";
                            report+="<TD> Judge C Score </TD>";
                            report+="<TD> Summary A </TD>";
                            report+="<TD> Summary B </TD>";
                            report+="<TD> Summary C </TD>";
                            report+="<TD> Deductions </TD>";
                            report+="<TD> Reason </TD>";                            
                            report+="<TD> Final Score </TD>";
                            
                            
                            report+="<TD> Ranking </TD>";
                            for (k = 0; k < vec.size();k++)
                            {
                                AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));

                                //create temporary athlete
                                athlete ath=new athlete(level, gender,globalData.eventInfo[i], ad.FirstName+" "+ad.LastName+" (0)");
                                //load all the data to the athlete stucture 
                                UnmarshalSimple(ath);

                                if(ad.Score.length()!=0)
                                {
                                    report+="<TR>";
                                        report+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                                        report+="<TD> "+ad.College+" </TD>";
                                        report+="<TD> "+ad.School+" </TD>"; 
                                        report+="<TD> "+gender+" </TD>";
                                        report+="<TD> "+level+" </TD>";
                                        report+="<TD> "+globalData.eventInfo[i]+" </TD>";
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ath.getAScoreSimple()))+" </TD>";
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ath.getBScoreSimple()))+" </TD>";
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ath.getCScoreSimple()))+" </TD>";
                                        report+="<TD> "+ad.JudgeAAgree+" </TD>";
                                        report+="<TD> "+ad.Time+" </TD>";
                                        report+="<TD> "+ad.JudgeCAgree+" </TD>";
                                        report+="<TD> "+ad.Deductions+" </TD>";
                                        report+="<TD> "+ad.Reason+" </TD>";
                                        report+="<TD> "+String.format("%.2f",getDoubleValue(ad.Score))+" </TD>";
                                        if(lastScore==getDoubleValue(ad.Score))
                                            report+="<TD> "+(k)+" </TD>";
                                        else
                                            report+="<TD> "+(k+1)+" </TD>";
                                        lastScore=getDoubleValue(ad.Score);
                                   report+="</TR>";
                                }
                            }
                        }
                    report+="</TABLE>";
                    report+="\r\n\r\n\r\n";
                    
                }
            } 
        }
        return report;        
    }
    
     //computes final report
    public String getReportSimple()
    {
       
        String report=new String();
        //print title
        report+="<font size=\"20\">Athletes Report\n\n</font>";

        for(int i=0;i< Array.getLength(globalData.AgeGroupList);i++)
        {
            for(int j=0;j< Array.getLength(globalData.LevelList);j++)
            {
                for(int k=0;k< Array.getLength(globalData.GenderList);k++)
                {
                    report+=getReportSimple(globalData.AgeGroupList[i],globalData.LevelList[j], globalData.GenderList[k]);
                }
            }
        }
        
        report+="<font size=\"20\">Team Trials Selection\n\n</font>";
        for(int i=0;i< Array.getLength(globalData.AgeGroupList);i++)
        {
            for(int j=0;j< Array.getLength(globalData.LevelList);j++)
            {
                for(int k=0;k< Array.getLength(globalData.GenderList);k++)
                {
                    
                    
                    String eventGroup1[][]={
                                                {"Changquan"},
                                                {"Broadsword"},
                                                {"Straightsword"},
						{"Nanquan","NanquanC","Nandao","Nangun"},
						{"TaijiWeapon","Taijiquan","TaijiquanC"},
                                            };
                    String eventGroup2[][]={
						{"ChangquanC"},
						{"Staff"},
						{"Spear"},
						{"Nanquan","NanquanC","Nandao","Nangun"},
						{"TaijiWeapon","Taijiquan","TaijiquanC"},
                                            };
                    for(int l=0;l<eventGroup1.length;l++)
                        report+=getEventRanking(globalData.AgeGroupList[i],globalData.LevelList[j], globalData.GenderList[k],eventGroup1[l],eventGroup2[l]);
                }
            }
        }
          
//        //Seperate TEAM A, TEAM B, TEAM C
//          //print title
//        report+="<font size=\"20\">Athletes Ranking\n\n</font>";
//
//        for(int i=0;i< Array.getLength(globalData.AgeGroupList);i++)
//        {
//            for(int j=0;j< Array.getLength(globalData.LevelList);j++)
//            {
//                for(int k=0;k< Array.getLength(globalData.GenderList);k++)
//                {
//                    report+=getRankingSimple(globalData.AgeGroupList[i],globalData.LevelList[j], globalData.GenderList[k]);
//                }
//            }
//        }

        return report;
    }
    
    //computes final report
    public String getScore()
    {
       
        String report=new String();
        //print title
        report+="<font size=\"20\">Athletes Report\n\n</font>";

        for(int i=0;i< Array.getLength(globalData.AgeGroupList);i++)
        {
            for(int j=0;j< Array.getLength(globalData.LevelList);j++)
            {
                for(int k=0;k< Array.getLength(globalData.GenderList);k++)
                {
                    report+=getScore(globalData.AgeGroupList[i],globalData.LevelList[j], globalData.GenderList[k]);
                }
            }
        }
        return report;
    }
    
    
    //returns report for an event
    private String getAthleteOrder(String ageGroup, String level,String gender,int[] eventID)
    {
        String report=new String();
        HashSet events = getEvents(ageGroup, level, gender);
        int i,j,k;
        if (events != null) 
        {
            for (i = 0; i < Array.getLength(globalData.eventInfo); i++) 
            {
                if (events.contains(globalData.eventInfo[i])) 
                {
                    Vector vec=new Vector();
                    Vector sortVec=new Vector();
                    for(k=0;k<AthleteList.size();k++)
                    {
                        AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(k);
                        if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level) && ad.Event.equals(globalData.eventInfo[i]))
                        {
                            vec.add(ad);
                        }
                    }
                    
                    //sort data
                    for (k = 0; k < vec.size();k++)
                        sortVec.add(k);
                    
                    for (j = 0; j < vec.size();j++)
                    {
                        for (k = j+1; k < vec.size();k++)
                        {
                            double a=0,b=0;
                            int x,y;
                            x=((Integer)sortVec.get(j));
                            y=((Integer)sortVec.get(k));
                            if(((AthleteFileData)vec.get(x)).Score.length()!=0)
                                a=getDoubleValue(((AthleteFileData)vec.get(x)).AthleteID);
                            
                            if(((AthleteFileData)vec.get(y)).Score.length()!=0)
                                b=getDoubleValue(((AthleteFileData)vec.get(y)).AthleteID);
                            
                            if(b>a)//switch
                            {
                                sortVec.set(j,y);
                                sortVec.set(k,x);
                            }
                        }
                    }
                    
                     //print 
                    report+="Athletic order "+" "+ageGroup+" "+level+" "+gender+" "+globalData.eventInfo[i]+"\r\n";
                    report+="<TABLE border=\"2\" cellpadding=\"8\">";

                    report+="<TD> Event ID </TD>";
                    report+="<TD> Order </TD>";
                    report+="<TD> Competitor Name </TD>";
                    //report+="<TD> School </TD>";
                    
                    for (k = 0; k < vec.size();k++)
                    {
                        AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));

                        //create temporary athlete
                        athlete ath=new athlete(level, gender,globalData.eventInfo[i], ad.FirstName+" "+ad.LastName+" (0)");
                        //load all the data to the athlete stucture 
                        Unmarshal(ath);
                       
                        report+="<TR>";
                            report+="<TD> "+(eventID[0]++)+" </TD>";
                            report+="<TD> "+(k+1)+" </TD>";
                            report+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                            //report+="<TD> "+ad.Coaches+" </TD>";
                        report+="</TR>";
                    }
                    report+="</TABLE>";
                    report+="\r\n\r\n\r\n";
                    
                }
            } 
        }
        return report;        
    }
    
     //returns report for an event
    private String getAthleteOrder(String ageGroup, String level,String gender,String event,int[] eventID)
    {
        String report=new String();

        Vector vec=new Vector();
        Vector sortVec=new Vector();
        for(int k=0;k<AthleteList.size();k++)
        {
            AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(k);
            if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level) && ad.Event.equals(event))
            {
                vec.add(ad);
            }
        }

        //sort data
        for (int k = 0; k < vec.size();k++)
            sortVec.add(k);

//        for (int j = 0; j < vec.size();j++)
//        {
//            for (int k = j+1; k < vec.size();k++)
//            {
//                double a=0,b=0;
//                int x,y;
//                x=((Integer)sortVec.get(j));
//                y=((Integer)sortVec.get(k));
//                if(((AthleteFileData)vec.get(x)).Score.length()!=0)
//                    a=getDoubleValue(((AthleteFileData)vec.get(x)).AthleteID);
//
//                if(((AthleteFileData)vec.get(y)).Score.length()!=0)
//                    b=getDoubleValue(((AthleteFileData)vec.get(y)).AthleteID);
//
//                if(b>a)//switch
//                {
//                    sortVec.set(j,y);
//                    sortVec.set(k,x);
//                }
//            }
//        }

        if(vec.size()>0)
        {
            //print 
           report+="Athletic order "+" "+ageGroup+" "+level+" "+gender+" "+event+"\r\n";
           report+="<TABLE border=\"2\" cellpadding=\"8\">";

           report+="<TD> Event ID </TD>";
           report+="<TD> Order </TD>";
           report+="<TD> Competitor Name </TD>";
           //report+="<TD> School </TD>";

           for (int k = 0; k < vec.size();k++)
           {
               AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));

               ////create temporary athlete
               //athlete ath=new athlete(level, gender,event, ad.FirstName+" "+ad.LastName+" (0)");
               //load all the data to the athlete stucture 
               //Unmarshal(ath);

               report+="<TR>";
                   report+="<TD> "+(eventID[0]++)+" </TD>";
                   report+="<TD> "+(k+1)+" </TD>";
                   report+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                   //report+="<TD> "+ad.Coaches+" </TD>";
               report+="</TR>";
           }
           report+="</TABLE>";
           report+="\r\n\r\n\r\n";
        }
        return report;       
    }
    
     //computes final report
    public String getAthleteOrder()
    {
        String report=new String();
        //print title
        report+="<font size=\"20\">Athletic Order\n\n</font>";

        int[] eventID = new int[1];
        eventID[0] = 0;

//        for(int i=0;i< Array.getLength(globalData.AgeGroupList);i++)
//        {
//            for(int j=0;j< Array.getLength(globalData.LevelList);j++)
//            {
//                for(int k=0;k< Array.getLength(globalData.GenderList);k++)
//                {
//                    report+=getAthleteOrder(globalData.AgeGroupList[i],globalData.LevelList[j], globalData.GenderList[k],eventID);
//                }
//            }
//        }
        
        
        for(int i=0;i< Array.getLength(globalData.AgeGroupList);i++)
        {
            for(int j=0;j< Array.getLength(globalData.LevelList);j++)
            {
                for(int l=0;l< Array.getLength(globalData.eventInfo);l++)
                {
                    for(int k=0;k< Array.getLength(globalData.GenderList);k++)
                    {
                        report+=getAthleteOrder(globalData.AgeGroupList[i],globalData.LevelList[j], globalData.GenderList[k],globalData.eventInfo[l],eventID);
                    }
                }
            }
        }
        return report;
    }
    
    //returns report for an event
    private String getAthleteOrderSimple(String ageGroup, String level,String gender,int[] eventID)
    {
        String report=new String();
        HashSet events = getEvents(ageGroup, level, gender);
        int i,j,k;
        if (events != null) 
        {
            for (i = 0; i < Array.getLength(globalData.eventInfo); i++) 
            {
                if (events.contains(globalData.eventInfo[i])) 
                {
                    Vector vec=new Vector();
                    Vector sortVec=new Vector();
                    for(k=0;k<AthleteList.size();k++)
                    {
                        AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(k);
                        if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level) && ad.Event.equals(globalData.eventInfo[i]))
                        {
                            vec.add(ad);
                        }
                    }
                    
                    //sort data
                    for (k = 0; k < vec.size();k++)
                        sortVec.add(k);
                    
                    for (j = 0; j < vec.size();j++)
                    {
                        for (k = j+1; k < vec.size();k++)
                        {
                            double a=0,b=0;
                            int x,y;
                            x=((Integer)sortVec.get(j));
                            y=((Integer)sortVec.get(k));
                            if(((AthleteFileData)vec.get(x)).Score.length()!=0)
                                a=getDoubleValue(((AthleteFileData)vec.get(x)).AthleteID);
                            
                            if(((AthleteFileData)vec.get(y)).Score.length()!=0)
                                b=getDoubleValue(((AthleteFileData)vec.get(y)).AthleteID);
                            
                            if(b>a)//switch
                            {
                                sortVec.set(j,y);
                                sortVec.set(k,x);
                            }
                        }
                    }
                    
                     //print 
                    report+="Athletic order "+" "+ageGroup+" "+level+" "+gender+" "+globalData.eventInfo[i]+"\r\n";
                    report+="<TABLE border=\"2\" cellpadding=\"8\">";

                    report+="<TD> Event ID </TD>";
                    report+="<TD> Order </TD>";
                    report+="<TD> Competitor Name </TD>";
                    //report+="<TD> School </TD>";
                    
                    for (k = 0; k < vec.size();k++)
                    {
                        AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));

                        //create temporary athlete
                        athlete ath=new athlete(level, gender,globalData.eventInfo[i], ad.FirstName+" "+ad.LastName+" (0)");
                        //load all the data to the athlete stucture 
                        UnmarshalSimple(ath);
                       
                        report+="<TR>";
                            report+="<TD> "+(eventID[0]++)+" </TD>";
                            report+="<TD> "+(k+1)+" </TD>";
                            report+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                            //report+="<TD> "+ad.Coaches+" </TD>";
                        report+="</TR>";
                    }
                    report+="</TABLE>";
                    report+="\r\n\r\n\r\n";
                    
                }
            } 
        }
        return report;        
    }
    
     //computes final report
    public String getAthleteOrderSimple()
    {
        String report=new String();
        //print title
        report+="<font size=\"20\">Athletic Order\n\n</font>";

        int[] eventID = new int[1];
        eventID[0] = 0;

        for(int i=0;i< Array.getLength(globalData.AgeGroupList);i++)
        {
            for(int j=0;j< Array.getLength(globalData.LevelList);j++)
            {
                for(int k=0;k< Array.getLength(globalData.GenderList);k++)
                {
                    report+=getAthleteOrderSimple(globalData.AgeGroupList[i],globalData.LevelList[j], globalData.GenderList[k],eventID);
                }
            }
        }
        return report;
    }
    
    //returns athletes nandu
    private String getAthletesNandu(String ageGroup, String level,String gender)
    {
        String report=new String();
        HashSet events = getEvents(ageGroup,level, gender);
        int i,j,k;
        if (events != null) 
        {
            for (i = 0; i < Array.getLength(globalData.eventInfo); i++) 
            {
                if (events.contains(globalData.eventInfo[i])) 
                {
                    //get the type of the scoring sytem used
                    //skip if traditional system is not nandu
                    if(getScoringSystem(level,globalData.eventInfo[i])!=2)
                        continue;
                    
                    Vector vec=new Vector();
                    Vector sortVec=new Vector();
                    for(k=0;k<AthleteList.size();k++)
                    {
                        AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(k);
                        if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level) && ad.Event.equals(globalData.eventInfo[i]))
                        {
                            vec.add(ad);
                        }
                    }
                    
                    //sort data
                    for (k = 0; k < vec.size();k++)
                        sortVec.add(k);
                    
                    for (j = 0; j < vec.size();j++)
                    {
                        for (k = j+1; k < vec.size();k++)
                        {
                            double a=0,b=0;
                            int x,y;
                            x=((Integer)sortVec.get(j));
                            y=((Integer)sortVec.get(k));
                            if(((AthleteFileData)vec.get(x)).Score.length()!=0)
                                a=getDoubleValue(((AthleteFileData)vec.get(x)).AthleteID);
                            
                            if(((AthleteFileData)vec.get(y)).Score.length()!=0)
                                b=getDoubleValue(((AthleteFileData)vec.get(y)).AthleteID);
                            
                            if(b>a)//switch
                            {
                                sortVec.set(j,y);
                                sortVec.set(k,x);
                            }
                        }
                    }
                                       
                    
                     //print 
                    report+="Athletic order "+level+" "+gender+" "+globalData.eventInfo[i]+"\r\n";
                    
                    
                    for (k = 0; k < vec.size();k++)
                    {
                        
                        AthleteFileData ad=(AthleteFileData)vec.get(((Integer)sortVec.get(k)));

                        //create temporary athlete
                        //athlete ath=new athlete(level, gender,globalData.eventInfo[i], ad.FirstName+" "+ad.LastName+" (0)");
                        //load all the data to the athlete stucture 
                        //Unmarshal(ath);

                        athlete at=new athlete("","",globalData.eventInfo[i],"");
                        at.setName(ad.FirstName+" "+ad.LastName);
                        Unmarshal(at);

                        report+="<TABLE border=\"2\" cellpadding=\"8\">";

                            report+="<tr>";
                                report+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                                report+="<TD> Description </TD>";
                                report+="<TD> Points </TD>";
                            report+="</tr>";

                            double totalPoints=0.0;
                            for(int ii=0;ii<at.getNanduCount();ii++)
                            {
                                report+="<tr>";

                                    String nanduCode=at.getNandu(ii);
                                    nanduData.nandu nanduDesc=globalData.nanduList.find(nanduCode);

                                    report+="<TD> "+nanduCode+" </TD>";
                                    report+="<TD> "+nanduDesc.description+" </TD>";
                                    report+="<TD> "+nanduDesc.points+" </TD>";

                                    int totalMovements=nanduDesc.getTotalMovements();

                                    for(int jj=0;jj<totalMovements;jj++)
                                        totalPoints+=nanduDesc.getPoint(jj);

                                report+="</tr>";
                            }

                            report+="<tr>";
                                report+="<TD> Grand </TD>";
                                report+="<TD> Total </TD>";
                                report+="<TD> "+String.format("%.2f", totalPoints)+" </TD>";
                            report+="</tr>";


                        report+="</TABLE>";
                        report+="\n\n";

                        
                        report+="<TABLE border=\"2\" cellpadding=\"8\">";
                            //report+="<tr><TD> Name </TD></tr>";
                            report+="<tr>";
                            report+="<TD> "+ad.FirstName+" "+ad.LastName+" </TD>";
                             //report+="<tr><TD>"+level+" "+gender+" "+globalData.eventInfo[i]+"</TD></tr>";

                            
                            
                            int cellcount = 0;
                            
                            //report += "<tr>";
                            //report +="<td></td>";
                            for(int ii=0;ii<at.getNanduCount();ii++)
                            {
                                    //report+="<TD> "+(ii+1)+" </TD>";
                                    //report+="<TD> "+at.getNandu(ii)+" </TD>";
                                    StringTokenizer tk = new StringTokenizer(at.getNandu(ii), "+", true);
                                        boolean lastWasPlus = false;
                                    while (tk.hasMoreTokens()) {
                                        String temptoken = tk.nextToken();

                                        if (!temptoken.equals("+")) {
                                            if (temptoken.length() == 1) {
                                                temptoken = "+" + temptoken;
                                            }
                                            else {
                                                if (lastWasPlus) {
                                                    report += "<td>+</td>";
                                                    cellcount++;
                                                }
                                            }
                                            report += "<td>" + temptoken + "</td>";
                                            lastWasPlus = false;
                                            cellcount++;
                                        }
                                        else {
                                            lastWasPlus = true;
                                        }
                                    }
                                    //report+="<TD>  </TD>";
                            }
                            report += "</tr>";
                            report += "<tr>";
                            report +="<td></td>";
                            for (int iii = 0; iii < cellcount; iii++) {
                                report += "<td>&nbsp;</td>";
                            }
                            report += "</tr>";
                        report+="</TABLE>";
                        report+="\r\n\r\n\r\n";
                    }
                    
                    
                }
            } 
        }
        return report;        
    }
    
     //computes final report
    public String getAthletesNandu()
    {
        String report=new String();
        //print title
        report+="<font size=\"20\">Athletes Nandu\n\n</font>";

         for(int i=0;i< Array.getLength(globalData.AgeGroupList);i++)
        {
            for(int j=0;j< Array.getLength(globalData.LevelList);j++)
            {
                for(int k=0;k< Array.getLength(globalData.GenderList);k++)
                {
                    report+=getAthletesNandu(globalData.AgeGroupList[i],globalData.LevelList[j], globalData.GenderList[k]);
                }
            }
        }
        return report;
    }
    
    
    //returns list of athlte in a event
    public Vector getAthletes(String ageGroup, String level, String gender, String ev)
    {
        Vector list = new Vector();
        int index=0;
        for(int i=0;i<AthleteList.size();i++)
        {
            AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(i);
            if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level) && ad.Event.equals(ev))
                list.addElement(ad.FirstName+" "+ad.LastName+" ("+(++index)+")");
        }
        return list;
    }
    
 
    //saves ordering to a file
    public void getAthletes(String ageGroup, String level, String gender, String ev,String fileName)
    {
        Vector list=getAthletes(ageGroup,level,gender,ev);
        try {
                PrintWriter f = new PrintWriter(new FileOutputStream(fileName, true));
                int r;
                f.println(level + " " + gender);
                f.println(ev);
                f.println("------------------------");
                for (r = 0; r < list.size(); r++) {
                    String temp = (String)list.get(r);
                    if(temp==null || temp.length()==0)    //this error comes while testing the incomplete database
                        continue;
                    String name = temp.substring(0, temp.lastIndexOf('(') - 1);
                    f.println("(" + (r+1) + ") " + name);
                }
                f.println();
                f.close();
            } catch (FileNotFoundException e) {
            }
    }
    
    //returns the type of the scoring system
    // 0 Traditional System := TraditionalSystem
    // 1 Compulsary Nandu := CompulsorySystem
    // 2 Nandu System := NanduSystem
    public int getScoringSystem(String level, String eventName)
    {
        for(int i=0; i<globalData.scoringSystemInfo.length;i++)
        {
            if(globalData.scoringSystemInfo[i].eventName.equals(eventName) &&
                    globalData.scoringSystemInfo[i].level.equals(level))
            {
                if(globalData.scoringSystemInfo[i].scoringType.equals("TraditionalSystem"))
                    return 0;
                else if(globalData.scoringSystemInfo[i].scoringType.equals("CompulsorySystem"))
                    return 1;
                else if(globalData.scoringSystemInfo[i].scoringType.equals("NanduSystem"))
                    return 2;
                return 2;
            }
        }
        return 0;
    }
    
    //returns true if the event is bare hand system
    public boolean isBareHandEvent(String eventName)
    {
        for(int i=0; i<globalData.BareHandEventName.size();i++)
        {
            if(((String)globalData.BareHandEventName.get(i)).equals(eventName))
                return true;
        }        
        return false;
    }
    
     //Athlete ID and entire event competed
    public AthleteReport getAthleteReport(String AthleteID)
    {
    
        AthleteReport ar=new AthleteReport();
         for(int i=0;i<AthleteList.size();i++)
        {
            AthleteFileData ad=(AthleteFileData)AthleteList.elementAt(i);
            if(ad.AthleteID.equals(AthleteID))
            {
                ar.AthleteEvents.addElement(ad);
            }
        }
        return ar;
    }
   

 }
